package com.blazingredfireball.rouleurlog.ria.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.blazingredfireball.rouleurlog.ria.business.RouteDelegate;
	import com.blazingredfireball.rouleurlog.ria.model.RouleurlogModelLocator;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	public class DeleteRouteCommand extends BaseCommand implements ICommand, IResponder
	{
		[Bindable]
		private var model : RouleurlogModelLocator = RouleurlogModelLocator.getInstance();
		
		public function execute( event : CairngormEvent ) : void
		{
		    var delegate : RouteDelegate = new RouteDelegate( this );    
		    delegate.deleteRoute( model.selectedRoute.id );
		}
		
		public function result( data : Object ) : void
		{
			var routeCollection : ArrayCollection =  model.routes;
			routeCollection.removeItemAt( routeCollection.getItemIndex( model.selectedRoute ));
			model.selectedRoute = null;
		}
		
		public function fault( info : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( info );
			
		    if (!checkForAuthenticationFault(faultEvent))
		    {
		    	Alert.show("Unable to delete route!");
		    }
		}
	}
}